package aom.ju.ss.view.base;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import aom.ju.ss.R;
import aom.ju.ss.utils.LogUtils;
import aom.ju.ss.utils.ThreadUtils;


/**
 * 自定义指示器
 */
public class MyIndicator extends View {
    public void setIndicatorSpacing(float indicatorSpacing) {
        this.indicatorSpacing = indicatorSpacing;
        invalidate();
    }

    public void setDefbg(int defbg) {
        this.defbg = defbg;
        bitmap = getBitmap(defbg);
        invalidate();
    }

    public void setDefIcon(int defIcon) {
        this.defIcon = defIcon;
        zs = getBitmap(defIcon);
        invalidate();
    }

    private float indicatorSpacing = 1.5f;//单个间距 百分比（指示之间的间距 不能小于1）可修改

    private int defbg = R.drawable.yuan_white;//未选中的图片 可修改

    private int defIcon = R.drawable.yuan_white;//选中的图片 可修改

    private Bitmap bitmap, zs;// 默认指示器 可滑动指示器(无需修改)

    private int indicatorWidth = 0;//单个指示器的宽度（仅做记录无需修改）

    private float lastValue = 0;//当前指示器之前的位置百分比 判断滑动方向所用（仅做记录无需修改）

    private int jianjuL;//指示器被滑动之前的位置（仅做记录无需修改）

    private int wjianju;//每次滑滑动 松开时到结束 像素值 (仅做记录无需修改)

    private float space = -999;// 单个间距 具体值（仅做记录无需修改）

    private int pageSize = 1;//指示数量 不能小于1（仅做记录无需修改）

    private float xz = 0;//选中的位置 （仅做记录无需修改）

    public MyIndicator(Context context) {
        super(context);
        initView();
    }

    public MyIndicator(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MyIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();


    }

    private void initView() {
        if (bitmap == null) {
            bitmap = getBitmap(defbg, R.color.ff64);
        }
    }

    private Bitmap getBitmap(int vectorDrawableId, int corId) {
        Bitmap bitmap = getBitmap(vectorDrawableId);

        ThreadUtils.INSTANCE.get().execute(() -> {
            for (int i = 0; i < bitmap.getWidth(); i++) {
                for (int j = 0; j < bitmap.getHeight(); j++) {
                    int c = bitmap.getPixel(i, j);
                    if (c != 0x000000) {
                        bitmap.setPixel(i, j, getResources().getColor(corId));
                    }
                }
            }
        });


        return bitmap;

    }

    private Bitmap getBitmap(int vectorDrawableId) {
        Bitmap bitmaps;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            Drawable vectorDrawable = getContext().getDrawable(vectorDrawableId);
            bitmaps = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
                    vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmaps);
            vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            vectorDrawable.draw(canvas);
        } else {
            bitmaps = BitmapFactory.decodeResource(getContext().getResources(), vectorDrawableId);
        }
        return bitmaps;
    }


    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;

    }

    public void setViewPager(ViewPager viewPager) {
        if (viewPager == null) {
            return;
        }

        pageSize = viewPager.getAdapter().getCount();

        zs = getBitmap(defIcon);
        indicatorWidth = zs.getWidth();
        if (space == -999) {
            space = bitmap.getWidth() * (indicatorSpacing - 1);
        }

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


                jianjuL = (int) space * position;

                if (lastValue > positionOffset) {
                    xz = (indicatorWidth * position) + jianjuL + ((wjianju * positionOffset));
                    // 递减，从左向右滑动
                } else if (lastValue < positionOffset) {

                    xz = (indicatorWidth * position) + jianjuL + ((wjianju * positionOffset));
                    // 递增，从右向左滑动
                }
                lastValue = positionOffset;


                invalidate();

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                wjianju = (int) space + indicatorWidth;//;
            }
        });
//        invalidate();
//        int dianL = bitmap.getWidth() * pageSize;//所有指示器的宽
//        int jianjuL = (int) (bitmap.getWidth() * (indicatorSpacing - 1) * (pageSize - 1));//所有间距的宽
//
//        LogUtils.e("2222222222pageSize--" + pageSize);
//        LogUtils.e("2222222222dianL" + dianL);
//        LogUtils.e("2222222222jianjuL" + jianjuL);

//        setMeasuredDimension(dianL + jianjuL, bitmap.getHeight());

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec , heightMeasureSpec);
        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        setMeasuredDimension(width, height);

    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //wrap_content
        if (specMode == MeasureSpec.AT_MOST) {
            specSize = (int) (bitmap.getWidth() * (pageSize * indicatorSpacing));
        }
        //fill_parent或者精确值
        else if (specMode == MeasureSpec.EXACTLY) {
        }
        return specSize;
    }

    //根据xml的设定获取高度
    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //wrap_content
        if (specMode == MeasureSpec.AT_MOST) {
            specSize=bitmap.getHeight();

        }
        //fill_parent或者精确值
        else if (specMode == MeasureSpec.EXACTLY) {
            specSize=bitmap.getHeight();
        }
        return specSize;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        for (int i = 0; i < pageSize; i++) {
            canvas.drawBitmap(bitmap, (bitmap.getWidth() * (i * indicatorSpacing)), 0, null);
        }

        if (zs != null) {
            canvas.drawBitmap(zs, xz, 0, null);
        }

    }


}
